﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Authentication.Test
{
    class AuthenticationTokenTest
    {
        public string Name{get;set;}
        public int? Id { get; set; }
        public double Price { get; set; }
        public DateTime CreateDate{get;set;}
        public int? NullableValue{get;set;}
        public Guid SystemId { get; set; }
        public static AuthenticationTokenTest Create(int? id, double price, DateTime createDate)
        {
            AuthenticationTokenTest obj = new AuthenticationTokenTest();
            obj.Name = "Test";
            obj.Id = id;
            obj.Price = price;
            obj.CreateDate = createDate;
            obj.SystemId = Guid.NewGuid();
            return obj;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            AuthenticationTokenTest tokenValues = AuthenticationTokenTest.Create(1,1.5,DateTime.Now);
            Console.WriteLine("------------------ Before encrypting --------------------");
            Console.WriteLine("Id:" + tokenValues.Id);
            Console.WriteLine("Price:" + tokenValues.Price);
            Console.WriteLine("CreateDate:" + tokenValues.CreateDate);
            Console.WriteLine("NullableValue:" + tokenValues.NullableValue);
            Console.WriteLine("SystemId:" + tokenValues.SystemId.ToString());
            
            string eToken = AuthenticationToken.AuthenticationToken.Generate<AuthenticationTokenTest>(tokenValues, "p9Ab#zTy");
            //It generates token as 
            //Name»Test«Id»1«Price»1.5«CreateDate»12/02/2009 12:17:01 PM«NullableValue»«SystemId»cd399a66-5614-4c6f-822c-6b4c05731f7d«
            Console.WriteLine("------------------ After encrypting --------------------");
            Console.WriteLine("Id:" + tokenValues.Id);
            Console.WriteLine("Price:" + tokenValues.Price);
            Console.WriteLine("CreateDate:" + tokenValues.CreateDate);
            Console.WriteLine("NullableValue:" + tokenValues.NullableValue);
            Console.WriteLine("SystemId:" + tokenValues.SystemId.ToString());
            
            tokenValues = AuthenticationTokenTest.Create(11, 1111.5, DateTime.Now.AddDays(10).AddHours(5));
            Console.WriteLine("------------------ After changing --------------------");
            Console.WriteLine("Id:" + tokenValues.Id);
            Console.WriteLine("Price:" + tokenValues.Price);
            Console.WriteLine("CreateDate:" + tokenValues.CreateDate);
            Console.WriteLine("NullableValue:" + tokenValues.NullableValue);
            Console.WriteLine("SystemId:" + tokenValues.SystemId.ToString());
            
            AuthenticationToken.AuthenticationToken.Parse<AuthenticationTokenTest>(ref tokenValues, eToken, "p9Ab#zTy");
            Console.WriteLine("------------------ After decrypting --------------------");
            Console.WriteLine("Id:" + tokenValues.Id);
            Console.WriteLine("Price:" + tokenValues.Price);
            Console.WriteLine("CreateDate:" + tokenValues.CreateDate);
            Console.WriteLine("NullableValue:" + tokenValues.NullableValue); // dispalys 0 because its value not available in token
            Console.WriteLine("SystemId:" + tokenValues.SystemId.ToString());
            
            Console.WriteLine();
            Console.ReadKey();
        }
    }
}
